import {createRouter, createWebHistory} from "vue-router";
import { ElMessage } from "element-plus";
import Login from "@/views/Login/index.vue";
import StudentLayout from "@/views/student/Layout.vue";
import SchoolLayout from "@/views/school/Layout.vue"; // 新建学校端布局

// 学生端路由
import StudentInfo from "@/views/student/StudentInfo.vue";
import ActivityList from "@/views/student/ActivityList.vue";
import EnrolledActivities from "@/views/student/EnrolledActivities.vue";
import UpdatePassword from "@/views/student/UpdatePassword.vue";

// 学校端路由（需要新建）
import SchoolDashboard from "@/views/school/Dashboard.vue";
import SchoolActivityManage from "@/views/school/ActivityManage.vue";
import SchoolActivityCheck from "@/views/school/ActivityCheck.vue";
import SchoolStudentManage from "@/views/school/StudentManage.vue";
import SchoolEmployeeManage from "@/views/school/EmployeeManage.vue";
import SchoolFeedManage from "@/views/school/FeedManage.vue";
import SchoolEmployeeInfo from "@/views/school/EmployeeInfo.vue";
import SchoolUpdatePassword from "@/views/school/UpdatePassword.vue";

// 定义路由关系
const routes = [
  { path: '/login', component: Login },
  
  // 学生端路由
  {
    path: '/student',
    component: StudentLayout,
    meta: { requireAuth: true, role: 'student' },
    children: [
      { path: 'activity', component: ActivityList },
      { path: 'enrolled', component: EnrolledActivities },
      { path: 'info', component: StudentInfo },
      { path: 'resetPassword', component: UpdatePassword },
      { path: '', redirect: '/student/activity' }
    ]
  },
  
  // 学校端路由
  {
    path: '/school',
    component: SchoolLayout,
    meta: { requireAuth: true, role: 'school' },
    children: [
      { path: 'dashboard', component: SchoolDashboard },
      { path: 'employee', component: SchoolEmployeeManage },
      { path: 'activity', component: SchoolActivityManage },
      { path: 'activity-check', component: SchoolActivityCheck },
      { path: 'student', component: SchoolStudentManage },
      { path: 'feedback', component: SchoolFeedManage },
      { path: 'info', component: SchoolEmployeeInfo },
      { path: 'resetPassword', component: SchoolUpdatePassword },
      { path: '', redirect: '/school/dashboard' }
    ]
  },
  
  // 默认重定向到登录页
  { path: '/', redirect: '/login' },
  { path: '/:pathMatch(.*)*', redirect: '/login' }
]

// 创建路由器
const router = createRouter({
  history: createWebHistory(), // 路由模式
  routes: routes
})

// 增强的路由守卫
router.beforeEach((to, from, next) => {
  // 登录页直接放行
  if (to.path === '/login') {
    return next();
  }

  const token = localStorage.getItem('token');
  const userRole = localStorage.getItem('userRole');
  
  // 检查是否登录
  if (!token) {
    return next('/login');
  }
  
  // 检查路由权限
  if (to.meta.requireAuth && to.meta.role && to.meta.role !== userRole) {
    ElMessage.error('无权访问该页面');
    // 跳转到对应身份的主页
    if (userRole === 'student') {
      return next('/student/activity');
    } else {
      return next('/school/dashboard');
    }
  }
  
  next();
});

//导出暴露
export default router
